package com.tmpt.utils.anno;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2017/9/12.
 */
public class MyDtoAssemble {

/*

    */
/**
     * 实体list load 到 dto中，启用注释过滤
     * @return
     * @throws IllegalAccessException
     *//*

    public static Financeaccountsummaryt1 dto2Bean(Financeaccountsummaryt1Dto dto) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        Financeaccountsummaryt1 bean = new Financeaccountsummaryt1();

        List<Field> dtoFieldList = new ArrayList<Field>();
        Field[] fields =  Financeaccountsummaryt1Dto.class.getDeclaredFields();
        for(Field field:fields){
            dtoFieldList.add(field);
        }

        for(Field dtoField:dtoFieldList){

            MyField myFieldAnn = dtoField.getAnnotation(MyField.class);
            MyFenAndYuan fen2YuanAnn = dtoField.getAnnotation(MyFenAndYuan.class);
            MyDateFormat  dateFormatAnn = dtoField.getAnnotation(MyDateFormat.class);
            MySecurityLevel securityLevelAnn = dtoField.getAnnotation(MySecurityLevel.class);
            MyOptions optionsAnn = dtoField.getAnnotation(MyOptions.class);

            if(myFieldAnn==null){
                continue;
            }
            boolean asParameter = myFieldAnn.asParameter();
            String fieldName = dtoField.getName();

            //取dto中field值
            dtoField.setAccessible(true);
            Object value = dtoField.get(dto);

            Field beanField = Financeaccountsummaryt1.class.getDeclaredField(fieldName);
            beanField.setAccessible(true);
            Class<?> fieldClass = beanField.getType();
            if(value==null || StringUtils.isNull((String)value)){continue;}

            if (fen2YuanAnn!=null) {
                Long fenLng = FenAndYuan.yuan2fenLng(String.valueOf(value));
                beanField.set(bean, fenLng);
                continue;
            } else if (dateFormatAnn != null) {
                Timestamp timestamp = DateTool.str2Timestamp((String) value, dateFormatAnn.format());
                beanField.set(bean, timestamp);
                continue;
            }else if (optionsAnn != null) {
                beanField.set(bean, Integer.parseInt(String.valueOf(value)));
                continue;
            } else {
                if(fieldClass.equals(String.class) )
                    beanField.set(bean, String.valueOf(value));
                if(fieldClass.equals(Integer.class) )
                    beanField.set(bean, Integer.parseInt(value.toString()));
                if(fieldClass.equals(Long.class) )
                    beanField.set(bean, Long.parseLong(value.toString()));
            }

        }


        return bean;
    }


    */
/**
     * 实体list load 到 dto中，启用注释过滤
     * @param securityLevel
     * @return
     * @throws IllegalAccessException
     *//*

    public static Financeaccountsummaryt1Dto bean2Dto(Financeaccountsummaryt1 bean,int securityLevel) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {


        List<Field> dtoFieldList = new ArrayList<Field>();
        Field[] fields =  Financeaccountsummaryt1Dto.class.getDeclaredFields();
        for(Field field:fields){
            dtoFieldList.add(field);
        }

        Financeaccountsummaryt1Dto dto = new Financeaccountsummaryt1Dto();
        for(Field dtoField:dtoFieldList){

        MyField myFieldAnn = dtoField.getAnnotation(MyField.class);
        MyFenAndYuan fen2YuanAnn = dtoField.getAnnotation(MyFenAndYuan.class);
        MyDateFormat  dateFormatAnn = dtoField.getAnnotation(MyDateFormat.class);
        MySecurityLevel securityLevelAnn = dtoField.getAnnotation(MySecurityLevel.class);
        MyOptions optionsAnn = dtoField.getAnnotation(MyOptions.class);

        if(myFieldAnn==null){
            continue;
        }
        if(myFieldAnn!=null && myFieldAnn.asParameter()){
            continue;
        }

        //取bean中field值
        String fieldName = dtoField.getName();
        Field beanField = Financeaccountsummaryt1.class.getDeclaredField(fieldName);
        beanField.setAccessible(true);
        Object value = beanField.get(bean);

        try {dtoField.setAccessible(true);
            if(securityLevelAnn!=null && securityLevelAnn.level()>securityLevel) {
                dtoField.set(dto,"");
                continue;
            }

            if (fen2YuanAnn != null) {
                String yuan = FenAndYuan.fen2yuanStr(value);
                dtoField.set(dto, yuan);
                continue;
            } else if (dateFormatAnn != null) {
                String dateStr = DateTool.date2String((java.util.Date) value, dateFormatAnn.format());
                dtoField.set(dto, dateStr);
                continue;
            }else if (optionsAnn != null) {
                String enumName = optionsAnn.enumName();
                String newFieldName = optionsAnn.newFieldName();
                //方法调用
                Class owner = Class.forName(enumName);
                Method method = owner.getMethod("explain", String.class, String.class);
                String dateStr =  (String)method.invoke(owner, String.valueOf(value),"");

                dtoField.set(dto, String.valueOf(value));
                Field field1 = Financeaccountsummaryt1Dto.class.getDeclaredField(newFieldName);
                field1.setAccessible(true);
                field1.set(dto,dateStr);
                continue;
            } else {
                dtoField.set(dto, String.valueOf(value));
            }

        }catch (Exception e){
            dtoField.set(dto, String.valueOf(value));
        }

        }


        return dto;
    }




    */
/**
     * 实体list load 到 dto中，启用注释过滤
     * @param list
     * @param securityLevel
     * @return
     * @throws IllegalAccessException
     *//*

    public static List<Financeaccountsummaryt1Dto> beanList2DtoList(List<Financeaccountsummaryt1> list,int securityLevel) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        List<Financeaccountsummaryt1Dto> dtoList = new ArrayList<Financeaccountsummaryt1Dto>();

        List<Field> dtoFieldList = new ArrayList<Field>();
        Field[] fields =  Financeaccountsummaryt1Dto.class.getDeclaredFields();
        for(Field field:fields){
            dtoFieldList.add(field);
        }
        for(Financeaccountsummaryt1 financeaccountsummaryt1:list){
            Financeaccountsummaryt1Dto dto = new Financeaccountsummaryt1Dto();
            for(Field dtoField:dtoFieldList){

                MyField myFieldAnn = dtoField.getAnnotation(MyField.class);
                MyFenAndYuan fen2YuanAnn = dtoField.getAnnotation(MyFenAndYuan.class);
                MyDateFormat  dateFormatAnn = dtoField.getAnnotation(MyDateFormat.class);
                MySecurityLevel securityLevelAnn = dtoField.getAnnotation(MySecurityLevel.class);
                MyOptions optionsAnn = dtoField.getAnnotation(MyOptions.class);

                if(myFieldAnn==null){
                    continue;
                }
                if(myFieldAnn!=null && myFieldAnn.asParameter()){
                    continue;
                }

                //取bean中field值
                String fieldName = dtoField.getName();
                Field beanField = Financeaccountsummaryt1.class.getDeclaredField(fieldName);
                beanField.setAccessible(true);
                Object value = beanField.get(financeaccountsummaryt1);

                try {dtoField.setAccessible(true);
                    if(securityLevelAnn!=null && securityLevelAnn.level()>securityLevel) {
                        dtoField.set(dto,"");
                        continue;
                    }

                    if (fen2YuanAnn != null) {
                        String yuan = FenAndYuan.fen2yuanStr(value);
                        dtoField.set(dto, yuan);
                        continue;
                    } else if (dateFormatAnn != null) {
                        String dateStr = DateTool.date2String((java.util.Date) value, dateFormatAnn.format());
                        dtoField.set(dto, dateStr);
                        continue;
                    }else if (optionsAnn != null) {
                        String enumName = optionsAnn.enumName();
                        String newFieldName = optionsAnn.newFieldName();
                        //方法调用
                        Class owner = Class.forName(enumName);
                        Method method = owner.getMethod("explain", String.class, String.class);
                        String dateStr =  (String)method.invoke(owner, String.valueOf(value),"");

                        dtoField.set(dto, String.valueOf(value));
                        Field field1 = Financeaccountsummaryt1Dto.class.getDeclaredField(newFieldName);
                        field1.setAccessible(true);
                        field1.set(dto,dateStr);
                        continue;
                    } else {
                        dtoField.set(dto, String.valueOf(value));
                    }

                }catch (Exception e){
                    dtoField.set(dto, String.valueOf(value));
                }

            }
            dtoList.add(dto);
        }


        return dtoList;
    }




    */
/**
     * 实体list load 到 dto中，启用注释过滤
     * @return
     * @throws IllegalAccessException
     *//*

    public static List<Financeaccountsummaryt1Dto> mapList2DtoList(List<Map> sumList) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        List<Financeaccountsummaryt1Dto> dtoList = new ArrayList<Financeaccountsummaryt1Dto>();

        List<Field> dtoFieldList = new ArrayList<Field>();
        Field[] fields =  Financeaccountsummaryt1Dto.class.getDeclaredFields();
        for(Field field:fields){
            dtoFieldList.add(field);
        }
        for(Map map:sumList){
            Financeaccountsummaryt1Dto dto = new Financeaccountsummaryt1Dto();
            for(Field dtoField:dtoFieldList){

                MyField myFieldAnn = dtoField.getAnnotation(MyField.class);
                MyFenAndYuan fen2YuanAnn = dtoField.getAnnotation(MyFenAndYuan.class);
                MyDateFormat  dateFormatAnn = dtoField.getAnnotation(MyDateFormat.class);
                MySecurityLevel securityLevelAnn = dtoField.getAnnotation(MySecurityLevel.class);
                MyOptions optionsAnn = dtoField.getAnnotation(MyOptions.class);

                if(myFieldAnn==null){
                    continue;
                }
                if(myFieldAnn!=null && myFieldAnn.asParameter()){
                    continue;
                }

                //取bean中field值
                String fieldName = dtoField.getName();
                Field beanField = Financeaccountsummaryt1.class.getDeclaredField(fieldName);
                beanField.setAccessible(true);
                Object value = map.get(fieldName);
                dtoField.setAccessible(true);


                try {
                    if (fen2YuanAnn != null) {
                        String yuan = FenAndYuan.fen2yuanStr(value);
                        dtoField.set(dto, yuan);
                        continue;
                    } else if (dateFormatAnn != null) {
                        String dateStr = DateTool.date2String((java.util.Date) value, dateFormatAnn.format());
                        dtoField.set(dto, dateStr);
                        continue;
                    }else if (optionsAnn != null) {
                        String enumName = optionsAnn.enumName();
                        String newFieldName = optionsAnn.newFieldName();
                        //方法调用
                        Class owner = Class.forName(enumName);
                        Method method = owner.getMethod("explain", String.class, String.class);
                        String dateStr =  (String)method.invoke(owner, String.valueOf(value),"");

                        dtoField.set(dto, String.valueOf(value));
                        Field field1 = Financeaccountsummaryt1Dto.class.getDeclaredField(newFieldName);
                        field1.setAccessible(true);
                        field1.set(dto,dateStr);
                        continue;
                    } else {
                        dtoField.set(dto, String.valueOf(value));
                    }
                }catch (Exception e){
                    dtoField.set(dto, String.valueOf(value));
                }

            }
            dtoList.add(dto);
        }


        return dtoList;
    }
*/




}
